package com.hspedu.spring.factory;

import com.hspedu.spring.bean.Monster;
import org.springframework.beans.factory.FactoryBean;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lihaojie
 * @Description: 是一个FactoryBean
 * @DateTime: 2024/2/19 15:43
 **/
public class MyFactoryBean implements FactoryBean<Monster> {

    // 这个就是你配置的时候 指定要获取的对象对应bean
    private String key;
    private Map<String, Monster> monster_map;

    {
        monster_map = new HashMap<>();
        monster_map.put("monster_01", new Monster(100, "黄袍怪", "一阳指"));
        monster_map.put("monster_05", new Monster(200, "九头金雕", "如来神掌"));
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public Monster getObject() throws Exception {
        return monster_map.get(key);
    }

    @Override
    public Class<?> getObjectType() {
        return Monster.class;
    }

    @Override
    public boolean isSingleton() { // 这里指定是否返回单例
        return true;
    }
}
